<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button class="filter-item" type="success" @click="handleSubmit"> 提交修改 </el-button>
    </div>
    <el-row :gutter="20">
      <el-col :xs="12" :sm="12" :lg="12" class="card-panel-col">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>基本信息</span>
          </div>
          <el-form :model="device">
            <el-form-item label="名称" :label-width="formLabelWidth">
              <el-input v-model="device.name" autocomplete="off" />
            </el-form-item>
            <el-form-item label="位置" :label-width="formLabelWidth">
              <el-input v-model="device.position" autocomplete="off" />
            </el-form-item>
            <el-form-item label="SN" :label-width="formLabelWidth">
              <el-input v-model="device.sn" autocomplete="off" />
            </el-form-item>
            <el-form-item label="订阅" :label-width="formLabelWidth">
              <el-input v-model="device.sub" autocomplete="off" />
            </el-form-item>
            <el-form-item label="推送" :label-width="formLabelWidth">
              <el-input v-model="device.pub" autocomplete="off" />
            </el-form-item>
            <el-form-item label="型号" :label-width="formLabelWidth">
              <el-input v-model="device.model" autocomplete="off" />
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>

      <el-col :xs="12" :sm="12" :lg="12" class="card-panel-col">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>设备配置</span>
          </div>
          <el-form :model="config" label-position="left" :label-width="formLabelWidth">
            <el-form-item>
              <el-button class="filter-item" type="success" @click="getConf"> 读取 </el-button>
              <el-button class="filter-item" type="primary" @click="setConf"> 设置 </el-button>
              <el-button class="filter-item" type="warning" @click="setReboot"> 重启 </el-button>
              <el-button class="filter-item" type="warning" @click="genAtCmd"> 生成AT命令 </el-button>
            </el-form-item>
            <el-form-item label="名称">
              <el-input v-model="config.name" autocomplete="off" />
            </el-form-item>
            <el-form-item label="版本">
              <el-input v-model="config.version" autocomplete="off" />
            </el-form-item>
            <el-form-item label="型号">
              <el-input v-model="config.model" autocomplete="off" />
            </el-form-item>
            <el-form-item label="创建时间">
              <el-date-picker v-model="config.create_time" type="datetime" placeholder="选择日期时间" value-format="yyyy-MM-dd HH:MM:SS" />
            </el-form-item>
            <el-form-item label="wifi连接名称">
              <el-input v-model="config.wifi_ssid" autocomplete="off" />
            </el-form-item>
            <el-form-item label="wifi连接密码">
              <el-input v-model="config.wifi_password" autocomplete="off" />
            </el-form-item>
            <el-form-item label="mqtt服务器地址">
              <el-input v-model="config.mqtt_server" autocomplete="off" />
            </el-form-item>
            <el-form-item label="mqtt端口">
              <el-input v-model="config.mqtt_port" autocomplete="off" />
            </el-form-item>
            <el-form-item label="mqtt客户端id">
              <el-input v-model="config.mqtt_clientid" autocomplete="off" />
            </el-form-item>
            <el-form-item label="mqtt订阅主题">
              <el-input v-model="config.mqtt_sub" autocomplete="off" />
            </el-form-item>
            <el-form-item label="mqtt订阅系统主题">
              <el-input v-model="config.mqtt_sub_sys" autocomplete="off" />
            </el-form-item>
            <el-form-item label="mqtt推送主题">
              <el-input v-model="config.mqtt_pub" autocomplete="off" />
            </el-form-item>
            <el-form-item label="mqtt用户名">
              <el-input v-model="config.mqtt_user" autocomplete="off" />
            </el-form-item>
            <el-form-item label="mqtt密码">
              <el-input v-model="config.mqtt_password" autocomplete="off" />
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
    </el-row>

    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
      <span>{{ at_config }}</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { editDevice } from '@/api/device'
import { CMD_DOOR } from '@/utils/cmd'

export default {
  name: 'Set',
  components: { },
  data() {
    return {
      formLabelWidth: '200px',
      dialogVisible: false,
      at_config: '',
      device: {},
      config: {}
    }
  },
  created() {
    this.device = this.$route.query
    console.log('设备信息')
    console.log(this.device)
    this.config = this.device.value
    this.subscribe(this.device.pub)
  },
  methods: {
    handleSubmit() {
      this.device.value = this.config
      console.log(this.device)
      editDevice(this.device).then(response => {
        if (response.code === 200) {
          this.$message({
            type: 'success',
            message: response.message
          })
          this.$router.push({ path: '/device/index' })
        } else this.$message(response.message)
      })
    },
    getConf() {
      this.config = {}
      this.publish(this.device.sub, JSON.stringify({
        code: CMD_DOOR.get_config.toString()
      }))
    },
    setConf() {
      this.publish(this.device.sub, JSON.stringify({
        code: CMD_DOOR.set_config.toString(),
        msg: this.config
      }))
    },
    setReboot() {
      this.publish(this.device.sub, JSON.stringify({
        code: CMD_DOOR.set_reboot.toString()
      }))
    },
    genAtCmd() {
      this.at_config = 'AT+CONF=' + JSON.stringify(this.config)
      this.dialogVisible = true
    },
    subscribe(val) {
      console.log('订阅')
      console.log('topic:' + val)
      this.$mqtt.subscribe(val)
    },
    publish(topic, message) {
      console.log('推送消息')
      console.log('topic:' + topic)
      console.log('message:' + message)
      this.$mqtt.publish(topic, message)
    }
  },
  mqtt: {
    '#'(data, topic) {
      console.log('收到消息')
      console.log('topic:' + topic)
      console.log('payload: ' + String.fromCharCode.apply(null, data))
      try {
        if (topic === this.device.pub) {
          var payload = JSON.parse(data)
          console.log(payload)
          console.log('主题正确')
          var aswcode = parseInt(payload.aswcode)

          if (aswcode === CMD_DOOR.get_config) {
            this.config = payload.aswmsg
            this.$message({
              message: '读取配置成功~',
              type: 'success'
            })
          }
          if (aswcode === CMD_DOOR.set_config) {
            this.$message({
              message: '设置成功~',
              type: 'success'
            })
          }
          if (aswcode === CMD_DOOR.set_reboot) {
            this.$message({
              message: '设备已重启~',
              type: 'success'
            })
          }
        }
      } catch (error) {
        this.$message({
          message: error,
          type: 'error'
        })
      }
    }
  }
}
</script>

<style scoped>
.box-card{
  margin-bottom: 20px;
}
</style>
